package com.deductive.web.disposition.wq;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.deductive.common.core.controller.BaseController;
import com.deductive.common.core.domain.AjaxResult;
import com.deductive.common.utils.DateUtils;
import com.deductive.domain.special.DispositionWqCompilation;
import com.deductive.domain.wq.DispositionWqWqllbz;
import com.deductive.service.wq.DispositionWqWqllbzService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 卫勤力量编组类.
 *
 * @author yexiaolong
 * @version 1.0
 * @since 2025/8/29
 */
@RestController
@RequestMapping("/medicalServiceStrengthGrouping")
@Api(tags = "WQ-卫勤力量编组管理")
public class MedicalServiceStrengthGroupingController extends BaseController {

    @Resource
    private DispositionWqWqllbzService dispositionWqWqllbzService;

    @GetMapping("/list")
    @ApiOperation("卫勤力量编组查询列表")
    public AjaxResult list(DispositionWqWqllbz hsll) {
        LambdaQueryWrapper<DispositionWqWqllbz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(hsll.getName()!=null,DispositionWqWqllbz::getName,hsll.getName());
        List<DispositionWqWqllbz> list = dispositionWqWqllbzService.list(queryWrapper);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setWeightKey("peopleNumber");
        List<Tree<Object>> build = TreeUtil.build(list, 0, treeNodeConfig, (map, tree) -> {
            tree.setId(map.getId());
            tree.setName(map.getName());
            tree.setParentId(map.getParentId());
            tree.putExtra("teamType", map.getTeamType());
            tree.putExtra("peopleNumber", map.getPeopleNumber());
            tree.putExtra("teamLevel", map.getTeamLevel());
            tree.putExtra("treatmentScope", map.getTreatmentScope());
            tree.putExtra("configurationLocation", map.getConfigurationLocation());
            tree.putExtra("affiliatedUnit", map.getAffiliatedUnit());
            tree.putExtra("parentNodeId", map.getParentId());
            tree.putExtra("relatedScenario", map.getRelatedScenario());
            tree.putExtra("remark", map.getRemark());
        });

        if (build == null || build.isEmpty()) {
            // 当没有可用的父子关系时，按平铺返回节点，避免返回空
            List<Tree<Object>> flat = new ArrayList<>();
            for (DispositionWqWqllbz map : list) {
                Tree<Object> tree = new Tree<>();
                tree.setId(map.getId());
                tree.setName(map.getName());
                tree.setParentId(map.getParentId());
                tree.putExtra("teamType", map.getTeamType());
                tree.putExtra("peopleNumber", map.getPeopleNumber());
                tree.putExtra("teamLevel", map.getTeamLevel());
                tree.putExtra("treatmentScope", map.getTreatmentScope());
                tree.putExtra("configurationLocation", map.getConfigurationLocation());
                tree.putExtra("affiliatedUnit", map.getAffiliatedUnit());
                tree.putExtra("parentNodeId", map.getParentId());
                tree.putExtra("relatedScenario", map.getRelatedScenario());
                tree.putExtra("remark", map.getRemark());
                flat.add(tree);
            }
            return AjaxResult.success(flat);
        }
        return AjaxResult.success(build);
    }

    @PutMapping("/update")
    @ApiOperation("卫勤力量编组修改")
    public AjaxResult update(@RequestBody DispositionWqWqllbz hsll) {
        try
        {
            hsll.setUpdateTime(DateUtils.getNowDate());
            return AjaxResult.success(dispositionWqWqllbzService.updateById(hsll));
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage(),false);
        }
    }

    @PostMapping("/insert")
    @ApiOperation("卫勤力量编组新增")
    public AjaxResult insert(@RequestBody DispositionWqWqllbz hsll) {
        try
        {
            hsll.setCreateTime(DateUtils.getNowDate());
            return AjaxResult.success(dispositionWqWqllbzService.save(hsll));
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage(),false);
        }
    }

    @DeleteMapping
    @ApiOperation("卫勤力量编组删除")
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return AjaxResult.success(dispositionWqWqllbzService.removeByIds(idList));
    }

}
